//
//  PlayViewController.m
//  EyesStroy
//
//  Created by lanou on 16/3/1.
//  Copyright (c) 2016年 刘洋. All rights reserved.
//

#import "PlayViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"
#import <MediaPlayer/MediaPlayer.h>
#import "playView.h"
typedef NS_ENUM(NSInteger, MoveMoment)
{
 
    HorizotailMovment,
    VerticaMovment
    
};
@interface PlayViewController ()
@property (nonatomic,strong)UILabel * leftLable;
@property (nonatomic,strong)UILabel * RightLable;
@property(nonatomic,strong)UISlider * Slider;
@property (nonatomic,strong)playView * myPlay;
@property (nonatomic,strong)UIButton * button;
@property (nonatomic,strong)UIButton * aginbutton;
@property (nonatomic,assign)BOOL isSele;
@property (nonatomic,strong)NSTimer * time;
@property (nonatomic,assign)BOOL isok;
@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _myPlay = [[playView alloc]initWithFrame:CGRectMake(0, 0, KedHeight ,   KedWidth) url:_string];
    [self.view addSubview:_myPlay];
    self.view.backgroundColor = [UIColor blackColor];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.myPlay addSubview:button];
    
;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(aaa:)];
    [self.myPlay addGestureRecognizer:tap];
    _myPlay.userInteractionEnabled = YES;
    _Slider = [[UISlider alloc]initWithFrame:CGRectMake(80 , KedWidth - 20, KedHeight - 160, 10)];
    _Slider.maximumValue = 5000;
    _Slider.minimumValue = 0;
    _Slider.value = _currTIme;
    [_Slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
    [_myPlay addSubview:_Slider];
    _leftLable = [[UILabel alloc]initWithFrame:CGRectMake(10, KedWidth - 30,60 , 20)];
    [_myPlay addSubview:_leftLable];
    _leftLable.textColor = [UIColor whiteColor];
    _RightLable =[[UILabel alloc]initWithFrame:CGRectMake(KedHeight - 70, KedWidth -30, 60, 20)];
    [_myPlay addSubview:_RightLable];
    _RightLable.textColor = [UIColor whiteColor];
    _RightLable.text = @"00:00";
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame =CGRectMake((KedHeight - 40)/2, (KedWidth - 40)/2, 40, 40);
    [_button setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(buuu:) forControlEvents:UIControlEventTouchUpInside];
    [_myPlay addSubview:_button];
    _time = [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(time:) userInfo:nil repeats:YES];
    
}
#pragma mark--- 界面将要出现
- (void)viewDidAppear:(BOOL)animated
{
      [self seekToTime:_currTIme];
}
#pragma mark ---点击消失
- (void)aaa: (UITapGestureRecognizer *)tap
{
    if (_isok == YES)
    {
      _button.hidden = NO;
        _leftLable.hidden = NO;
        _Slider.hidden = NO;
        _RightLable.hidden = NO;
    }
    else
    {
      _button.hidden = YES;
        _leftLable.hidden = YES;
        _Slider.hidden = YES;
        _RightLable.hidden = YES;
    }
    
    _isok = !_isok;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [_time invalidate];
    _time = nil;
}
#pragma mark --- 时时更新滑条
- (void)time:(NSTimer *)time
{
    _leftLable.text = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)(_myPlay.player.currentTime.value / _myPlay.player.currentTime.timescale )/60  ,(NSInteger)(_myPlay.player.currentTime.value /_myPlay.player.currentTime.timescale) %60];

    CMTime timer = _myPlay.player.currentItem.duration;
   ;    if (timer.timescale!=0)
    {
     _Slider.maximumValue = timer.value / timer.timescale;
        _RightLable.text = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)(timer.value / timer.timescale)/60,(NSInteger)(timer.value / timer.timescale)%60];
        
        if (timer.value == _myPlay.player.currentTime.value)
        {
            _button.hidden = YES;
            _leftLable.hidden = YES;
            _Slider.hidden = YES;
            _RightLable.hidden = YES;
            _aginbutton.hidden = NO;
            _aginbutton = [UIButton buttonWithType:UIButtonTypeSystem];
            _aginbutton.frame =CGRectMake((KedWidth - 80)/2, (KedHeight -40)/2, 80, 40);
            
           // [_aginbutton setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
            [_aginbutton setTitle:@"重新播放" forState:UIControlStateNormal];
            [_aginbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_aginbutton addTarget:self action:@selector(again:) forControlEvents:UIControlEventTouchUpInside];
            [_myPlay addSubview:_aginbutton];

            
        }
    
    }
    
    _Slider.value = _myPlay.player.currentTime.value/ _myPlay.player.currentTime.timescale;
  
}
#pragma mark --- 再次播放
- (void)again: (UIButton *)button
{
    _aginbutton.hidden = YES;
    [self seekToTime:0];
    
}
#pragma mark --- 暂停button
- (void)buuu: (UIButton *)sender
{
    if (_isSele == NO)
    {
       [_myPlay.player pause];
           [_button setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    }
    else
    {
        [_myPlay.player play];
        [_button setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    }
    _isSele = !_isSele;
    
}
#pragma mark---滑条触发
- (void)slider: (UISlider *)slider
{
    
    [self seekToTime:slider.value];
    
}
#pragma mark --- 返回
- (void)back: (UIButton *)sender
{
    [_myPlay.player pause];
    AppDelegate * aaa = [UIApplication sharedApplication].delegate;
    aaa.isOk = NO;
    [_myPlay.player pause];
    [self dismissViewControllerAnimated:NO completion:nil];
}
#pragma mark --- 调到某个时间
- (void)seekToTime :(NSInteger)curryime
{
   
    [_myPlay.player seekToTime:CMTimeMake(_myPlay.player.currentTime.timescale * curryime, _myPlay.player.currentTime.timescale)];
    [_myPlay.player play];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
